package com.example.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.exception.CustomException;
import com.example.pojo.MsslyyUser;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.MsslyyUserMapper;
import com.example.pojo.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class MsslyyUserService extends ServiceImpl<MsslyyUserMapper, MsslyyUser> {

    @Resource
    private MsslyyUserMapper msslyyUserMapper;

    public MsslyyUser login(MsslyyUser msslyyUser) {
        LambdaQueryWrapper<MsslyyUser> queryWrapper = Wrappers.<MsslyyUser>lambdaQuery().eq(MsslyyUser::getUserUsername, msslyyUser.getUserUsername()).eq(MsslyyUser::getUserPassword, msslyyUser.getUserPassword());
        MsslyyUser one = getOne(queryWrapper);
        if (one == null) {
            throw new CustomException("-1", "账号或密码错误");
        }
        return one;
    }

    public MsslyyUser register(MsslyyUser msslyyUser) {
        MsslyyUser one = getOne((Wrappers.<MsslyyUser>lambdaQuery().eq(MsslyyUser::getUserUsername, msslyyUser.getUserUsername())));
        if (one != null) {
            throw new CustomException("-1", "用户已注册");
        }
        if (msslyyUser.getUserPassword() == null) {
            msslyyUser.setUserPassword("123456");
        }
        save(msslyyUser);
        return getOne((Wrappers.<MsslyyUser>lambdaQuery().eq(MsslyyUser::getUserUsername, msslyyUser.getUserUsername())));
    }

}
